package com.xiaoshuidi.cloud.module.finance.dal.mysql.finance;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.mybatis.core.mapper.BaseMapperX;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.transfer.FinanceBankTransferPageReqVO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.FinanceBankTransferDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Mapper
public interface FinanceBankTransferMapper extends BaseMapperX<FinanceBankTransferDO> {

    default PageResult<FinanceBankTransferDO> selectPage(FinanceBankTransferPageReqVO reqVO) {
        return selectPage(reqVO, wrapperQuery(reqVO));
    }

    default QueryWrapper<FinanceBankTransferDO> wrapperQuery(FinanceBankTransferPageReqVO reqVO) {
        QueryWrapper<FinanceBankTransferDO> wrapper = new QueryWrapper<FinanceBankTransferDO>()
                .like(StringUtils.hasText(reqVO.getPayeeBank()), "payee_bank", reqVO.getPayeeBank())
                .like(StringUtils.hasText(reqVO.getPayeeBankCode()), "payee_bank_code", reqVO.getPayeeBankCode())
                .eq(StringUtils.hasText(reqVO.getStatus()), "status", reqVO.getStatus())
                .eq(StringUtils.hasText(reqVO.getPayeeType()), "payee_type", reqVO.getPayeeType())
                .eq(reqVO.getApartmentId() != null, "apartment_id", reqVO.getApartmentId())
                .eq(reqVO.getIsChange() != null, "is_change", reqVO.getIsChange())
                .and(StringUtils.hasText(reqVO.getKeyword()), qw -> qw.like("number", reqVO.getKeyword())
                        .or()
                        .like("contract_no", reqVO.getKeyword())
                        .or()
                        .like("biz_no", reqVO.getKeyword())
                        .or()
                        .like("payee_name", reqVO.getKeyword())
                        .or()
                        .like("payee_mobile", reqVO.getKeyword())
                        .or()
                        .like("payee_bank_account", reqVO.getKeyword())
                );
        if (reqVO.getCreateTime() != null && reqVO.getCreateTime().length > 1) {
            wrapper.between(reqVO.getCreateTime() != null && reqVO.getCreateTime().length > 1,
                    "create_time", reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        if (reqVO.getPayTime() != null && reqVO.getPayTime().length>1) {
            wrapper.between(reqVO.getPayTime() != null && reqVO.getPayTime().length > 1, "pay_time",
                    reqVO.getPayTime()[0], reqVO.getPayTime()[1]);
        }
        if (reqVO.getPayAmount() != null && reqVO.getPayAmount().length > 0) {
            wrapper.ge(reqVO.getPayAmount()[0] != null, "pay_amount", reqVO.getPayAmount()[0]);
        }
        if (reqVO.getPayAmount() != null && reqVO.getPayAmount().length > 1) {
            wrapper.le(reqVO.getPayAmount()[1] != null, "pay_amount", reqVO.getPayAmount()[1]);
        }
        return wrapper.orderByDesc("id");
    }

    default List<Map<String, Object>> selectConfirmSummary(FinanceBankTransferPageReqVO reqVO) {
        return selectMaps(wrapperQuery(reqVO)
                .select("max(id) id","status", "sum(pay_amount) as payAmount", "count(1) as cnt")
                .groupBy("status"));
    }

    /**
     * 导出统计
     *
     * @param ids
     * @return
     */
    int updateExportCount(@Param("ids") List<Integer> ids);
}
